package cn.wanyan.app升级;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map.Entry;
import java.util.Set;

public class GetLine {
	public static void main(String[] args) throws IOException {
		List<App> list1 = new ArrayList<>();
		List<String> list = new ArrayList<String>();
		LinkedHashMap<String, List<String>> map = new LinkedHashMap<String, List<String>>();
		//读取文件，获得集合
		getMap(map);
		// 新建对象。并放入list1中
		getFinal(list1, map);
		// 时间升序
		upTime(list1);
		//遍历最终结果
		for (App app : list1) {
			System.out.println(app);
		}
	}

	/**读取文件，获得集合
	 * @param map
	 */
	public static void getMap(LinkedHashMap<String, List<String>> map) {
		try (BufferedReader br = new BufferedReader(new FileReader("D:\\java案例专用\\app.txt"));) {
			// 把qian4个作为key，和版本信息添加到list中当作value存进map
			String st;
			while ((st = br.readLine()) != null) {
				String[] str = st.split(",");
				String date = str[0];
				String username = str[1];
				String appname = str[2];
				String storename = str[3];
				String verson = str[5];
				String key = date + "," + username + "," + appname + "," + storename;
				// 得到map集合
				getcollection(map, verson, key);
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 得到map集合
	 * 
	 * @param map
	 * @param verson
	 * @param key
	 */
	public static void getcollection(LinkedHashMap<String, List<String>> map, String verson, String key) {
		List<String> list;
		if (map.containsKey(key)) {
			list = map.get(key);
			list.add(verson);
			// map.put(key, list);
		} else {
			list = new ArrayList<String>();
			list.add(verson);
			map.put(key, list);
		}
	}

	/**
	 * 新建对象。并放入list1中
	 * 
	 * @param list1
	 * @param map
	 */
	public static void getFinal(List<App> list1, LinkedHashMap<String, List<String>> map) {
		List<String> list;
		String st;
		Set<Entry<String, List<String>>> entrySet = map.entrySet();
		for (Entry<String, List<String>> entry : entrySet) {
			list = entry.getValue();
			upList(list);// 对list进行排序
			// removeSame(list);//去重
			// getMaxMin(list);//得到最大值和最小值
			String maxverson = list.get(list.size() - 1);
			String minverson = list.get(0);
			// 对键进行分割
			st = entry.getKey();
			String[] str = st.split(",");
			String date = str[0];
			String username = str[1];
			String appname = str[2];
			String storename = str[3];

			list1.add(new App(date, username, appname, storename, minverson, maxverson));
			// System.out.println(entry.getKey() + ":" + list);
		}
	}

	/**
	 * 时间升序
	 * 
	 * @param list1
	 */
	public static void upTime(List<App> list1) {
		Collections.sort(list1, new Comparator<App>() {

			@Override
			public int compare(App o1, App o2) {
				Date date1 = new Date();
				Date date2 = new Date();
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
				try {

					date1 = sdf.parse(o1.getDate());
					date2 = sdf.parse(o2.getDate());

				} catch (ParseException e) {

					e.printStackTrace();
				}
				return date1.compareTo(date2);

			}
		});
	}

	/**
	 * 得到最大值和最小值
	 * 
	 * @param list
	 */
	public static void getMaxMin(List<String> list) {
		if (list.size() > 1) {
			if (list.size() > 2) {
				for (int i = 0; i < list.size(); i++) {
					if (i != 0 && i != list.size()) {
						list.remove(i);
					}
				}
			}
		}
	}

	/**
	 * 去除重复值
	 * 
	 * @param list
	 */
	public static void removeSame(List<String> list) {
		for (int i = 0; i < list.size() - 1; i++) {
			for (int j = i + 1; j < list.size(); j++) {
				if (list.get(i).equals(list.get(j))) {
					list.remove(i);
				}
			}
		}
	}

	/**
	 * 对list版本号集合升序
	 * 
	 * @param list
	 *            数组集合
	 */
	public static void upList(List<String> list) {
		Comparator<String> co = new Comparator<String>() {
			@Override
			public int compare(String o1, String o2) {

				return o1.compareTo(o2);
			}
		};
		Collections.sort(list, co);
	}
}
